
:-['tools.pro','globalConstraints.pro','pairwisegenerator.pro' ].




bench:- L1 = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15],
        L2 = [B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16,B17],
        L3 = [C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,C15,C16,C18,C19,C20,C21,C22,C23,C24,C25,C26,C27,C28,C29],
        Ctr= [domain(L1,-1,2),domain(L2,-1,1),domain(L3,-1,0)],
        append(L1,L2,L),append(L3,L,L0),
        minimizer0(L0,PrimitivesFeaturesList, Ctr, 'test',TimeOut, OutputFileMatrix, 'bench.txt',100) .

minimizer0(FeaturesList,PrimitivesFeaturesList, Constraints, Name,TimeOut, OutputFileMatrix, OutputFileStats,Size) :-
          problemTimeRef(FeaturesList, Constraints, TimeOut,  Size,SizeOut),
        
          minimizer(FeaturesList,PrimitivesFeaturesList, Constraints, Name, OutputFileMatrix, OutputFileStats,SizeOut,TimeOut)  .

minimizer(FeaturesList,PrimitivesFeaturesList, Constraints, Name, OutputFileMatrix, OutputFileStats,Size,TimeRef) :-
        T is TimeRef*3,
        problemeMinimizer(FeaturesList, Constraints, Name,T,Time2,OutputFileStats,Size,SizeOut)  ,
        number(SizeOut) ->
                SizeOut2 is SizeOut -1 ,
                (SizeOut2 == 3,open('statistics.txt', append, Stream),write(Stream,Name),write(Stream,';'),write(Stream,SizeOut),nl(Stream),close(Stream),halt;
                 T is TimeRef*10,T < Time2 ,open('statistics.txt', append, Stream),write(Stream,Name),write(Stream,';'),write(Stream,SizeOut),nl(Stream),close(Stream),halt  ;
                 minimizer(FeaturesList,PrimitivesFeaturesList, Constraints, Name, OutputFileMatrix, OutputFileStats,SizeOut2,TimeRef));
        halt  .


problemTimeRef(FeatureList, Constraints, TimeOut,  SizeIn,SizeOut) :-
        statistics(runtime, [T0| _]),
        callRec(Constraints),
        length(FeatureList,SizeMat),
        matrice(M,SizeMat,SizeIn),
        limitMatrix(M,FeatureList),
        pairwiseGenerator(FeatureList,M,PWCTRLST,RANKLST),
        length(PWCTRLST,Ls),
        writeln(Ls),
        callRec(PWCTRLST),
        contrainteFD(M,FeatureList), 
        alldiffrec(RANKLST),
        flatten(RANKLST,Ilist),
        domain(Ilist,1,SizeIn),
        maximum(Kn,Ilist),
        labeling([ff],Ilist),
        statistics(runtime, [T1| _]),       
        SizeOut = Kn,
        TimeOut is T1 - T0.


/*
Benchmark 
*/


/*minimizer(FeaturesList,PrimitivesFeaturesList, Constraints, Name,TimeOut, OutputFileMatrix, OutputFileStats,Size,TimeRef) :-problemeMinimizer(FeaturesList,PrimitivesFeaturesList, Constraints, Name,TimeOut, OutputFileMatrix, OutputFileStats,Size,SizeOut) ,SizeOut2 is SizeOut -1 ,(SizeOut2 == 3;minimizer(FeaturesList,PrimitivesFeaturesList, Constraints, Name,TimeOut, OutputFileMatrix, OutputFileStats,SizeOut2)).



minimizerRec(Cout,LF,LF2,StatsRes,Nom,Time,Size,success):- TimeIn is 2 * Time,writeln(TimeIn),time_out(problemeMinimizer2(Cout,LF,LF2,StatsRes,Nom,TimeIn,Size,_),TimeIn,Res),SizeOut2  is Size - 1, minimizerRec(Cout,LF,LF2,StatsRes,Nom,Time,SizeOut2,Res).
minimizerRec(_,_,_,_,_,_,_,time_out):-halt.   
*/

problemeMinimizer(FeaturesList, Constraints, Name,T,T2,OutputFileStats,Size,SizeOut) :-
        statistics(runtime, [T0| _]),    
        callRec(Constraints),
        length(FeaturesList,SizeMat),
        matrice(M,SizeMat,Size),
        limitMatrix(M,FeaturesList),
        pairwiseGenerator(FeaturesList,M,PWCTRLST,RANKLST),
           contrainteFD(M,FeaturesList), 
        callRec(PWCTRLST),
     
        alldiffrec(RANKLST),
        
        flatten(RANKLST,Ilist),
        domain(Ilist,1,Size),
        (labeling([ff,time_out(T,_)],Ilist);open('statistics.txt', append, Stream),Size0 is Size + 1,write(Stream,Name),write(Stream,';'),write(Stream,Size0),nl(Stream),close(Stream),halt),
        maximum(SizeOut,Ilist),
        statistics(runtime, [T1| _]),
        T2 is T1 - T0,            
        write(' I : '), writeln(SizeOut),
        fd_statistics(constraints, CtrNumber),
        fd_statistics(resumptions, Resumptions),
        fd_statistics(entailments, Entailments),
        fd_statistics(prunings, Prunings),
        fd_statistics(backtracks, Backtracks),
        writeStatMin(T2,SizeOut,CtrNumber,Resumptions,Entailments,Prunings,Backtracks,OutputFileStats).
   

writeStatMin(Temps,ValeurdeK,CtrNumber,Resumptions,Entailments,Prunings,Backtracks, File):-open(File, append, Stream),
                                                                                           write(Stream,ValeurdeK),
                                                                                           write(Stream,';'),
                                                                                           write(Stream,CtrNumber),
                                                                                           write(Stream,';'),
                                                                                           write(Stream,Resumptions),
                                                                                           write(Stream,';'),
                                                                                           write(Stream,Entailments),
                                                                                           write(Stream,';'),
                                                                                           write(Stream,Prunings),
                                                                                           write(Stream,';'),
                                                                                           write(Stream,Backtracks),
                                                                                           write(Stream,';'),
                                                                                           write(Stream,Temps),nl(Stream),close(Stream).








